home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 008a / perl40_2.zip / CHDIR.C < prev    next >
C/C++ Source or Header  |  1991-11-28  |  2KB  |  117 lines

  1. /*
  2.  *    (C) Copyright 1990, 1991 Tom Dinger
  3.  *
  4.  *    You may distribute under the terms of either the GNU General Public
  5.  *    License or the Artistic License, as specified in the README file.
  6.  *
  7.  */
  8.  
  9.  
  10. /*
  11.  * A "DOS-aware" chdir() function, that will change current drive as well.
  12.  *
  13.  *    chdir( "B:" )    -- changes to the default directory, on drive B:
  14.  *    chdir( "C:\FOO" )  changes to the specified directory, on drive C:
  15.  *    chdir( "\BAR" )    changes to the specified directory on the current
  16.  *               drive.
  17.  */
  18.  
  19.  
  20. #include <stdlib.h>
  21. #include <ctype.h>
  22. #include <direct.h>
  23. #include <dos.h>
  24. #include <errno.h>
  25.  
  26.  
  27. #include "config.h"
  28. #ifdef chdir
  29. #undef chdir
  30. #endif
  31.  
  32.  
  33. /* We should have the line:
  34.  *
  35.  * #define chdir perl_chdir
  36.  *
  37.  * in some header for perl (I put it in config.h) so that all
  38.  * references to chdir() become references to this function.
  39.  */
  40.  
  41.  
  42. /*------------------------------------------------------------------*/
  43.  
  44.  
  45. #if defined(BUGGY_MSC5)    /* only needed for MSC 5.1 */
  46.  
  47.  
  48. int _chdrive( int drivenum )
  49. {
  50. unsigned int    ndrives;
  51. unsigned int    tmpdrive;
  52.  
  53.  
  54.  
  55.  
  56. _dos_setdrive( drivenum, &ndrives );
  57.  
  58.  
  59. /* check for illegal drive letter */
  60. _dos_getdrive( &tmpdrive );
  61.  
  62.  
  63. return (tmpdrive != drivenum) ? -1 : 0 ;
  64. }
  65.  
  66.  
  67. #endif
  68.  
  69.  
  70. /*-----------------------------------------------------------------*/
  71.  
  72.  
  73. int perl_chdir( char * path )
  74. {
  75. int        drive_letter;
  76. unsigned int    drivenum;
  77.  
  78.  
  79.  
  80.  
  81. if ( path && *path && (path[1] == ':') )
  82.     {
  83.     /* The path starts with a drive letter */
  84.     /* Change current drive */
  85.     drive_letter = *path;
  86.     if ( isalpha(drive_letter) )
  87.     {
  88.     /* Drive letter legal */
  89.     if ( islower(drive_letter) )
  90.         drive_letter = toupper(drive_letter);
  91.     drivenum = drive_letter - 'A' + 1;
  92.  
  93.  
  94.     /* Change drive */
  95.     if ( _chdrive( drivenum ) == -1 )
  96.         {
  97.         /* Drive change failed -- must be illegal drive letter */
  98.         errno = ENODEV;
  99.         return -1;
  100.         }
  101.  
  102.  
  103.     /* Now see if that's all we do */
  104.     if ( ! path[2] )
  105.         return 0;        /* no path after drive -- all done */
  106.     }
  107.     /* else drive letter illegal -- fall into "normal" chdir */
  108.     }
  109.  
  110.  
  111. /* Here with some path as well */
  112. return chdir( path );
  113.  
  114.  
  115. /* end perl_chdir() */
  116. }
  117.